//! @author Adam Emil Skoog
//! @date   2012-01-05

#include "KeyInputEventReceiver.h"

namespace kedja
 {
	/**
	 * Constructor.
	 */
	KeyInputEventReceiver::KeyInputEventReceiver()
	:
	onKeyPress  (NULL),
	onKeyRelease(NULL)
	 {
		// Make sure that the class has been initialised.
		assert(static_cast<bool>(keyInputEventReceivers));

		if (keyInputEventReceivers)
			keyInputEventReceivers->push_back(this);
	 }

	/**
	 * Registers a function to invoke when this event is invoked,
	 * when a mouse button is pressed.
	 * 
	 * @param function The function to register.
	 */
	void KeyInputEventReceiver::registerOnKeyPress(void (*function)(const sf::Key::Code &))
	 {
		onKeyPress = function;
	 }

	/**
	 * Deregisters the function registered for this event.
	 */
	void KeyInputEventReceiver::deregisterOnKeyPress()
	 {
		onKeyPress = NULL;
	 }

	/**
	 * Registers a function to invoke when this event is invoked,
	 * when a mouse button is released.
	 * 
	 * @param function The function to register.
	 */
	void KeyInputEventReceiver::registerOnKeyRelease(void (*function)(const sf::Key::Code &))
	 {
		onKeyRelease = function;
	 }

	/**
	 * Deregisters the function registered for this event.
	 */
	void KeyInputEventReceiver::deregisterOnKeyRelease()
	 {
		onKeyRelease = NULL;
	 }

	/**
	 * Initialises the receiver's system.
	 */
	void KeyInputEventReceiver::initialise()
	 {
		// Prohibit double initialisation.
		assert(!static_cast<bool>(keyInputEventReceivers));

		if (!keyInputEventReceivers)
			keyInputEventReceivers = new std::vector<KeyInputEventReceiver *>;
	 }

	/**
	 * Deinitialises the receiver's system.
	 */
	void KeyInputEventReceiver::deinitialise()
	 {
		// Prohibit double deinitialisation.
		assert(static_cast<bool>(keyInputEventReceivers));

		if (keyInputEventReceivers)
		 {
			delete keyInputEventReceivers;
			keyInputEventReceivers = NULL;
		 }
	 }

	/**
	 * Returns the mouse input event receiver at the specified index.
	 *
	 * @param index The index from which to retrieve the receiver.
	 * @return The receiver at the specified index.
	 */
	KeyInputEventReceiver *const KeyInputEventReceiver::getKeyInputEventReceiver(const unsigned &index)
	 {
		// Make sure that the class has been initialised.
		assert(static_cast<bool>(keyInputEventReceivers));

		// Make sure there are any parents at all.
		assert((keyInputEventReceivers->size() > 0));

		// Validate the index.
		assert((index < keyInputEventReceivers->size()));

		if (keyInputEventReceivers)
			if (keyInputEventReceivers->size() > 0)
				if (index < keyInputEventReceivers->size())
					return (*keyInputEventReceivers)[index];

		return NULL;
	 }

	/**
	 * Returns the number of mouse input event receivers.
	 *
	 * @return The number of receivers.
	 */
	const unsigned KeyInputEventReceiver::getKeyInputEventReceiverCount()
	 {
		// Make sure that the class has been initialised.
		assert(static_cast<bool>(keyInputEventReceivers));

		if (keyInputEventReceivers)
			return keyInputEventReceivers->size();

		return 0;
	 }

	// Define static variables.
	std::vector<KeyInputEventReceiver *> *KeyInputEventReceiver::keyInputEventReceivers = NULL;
 }
